 <?php
 //use for showing tasks' data
    @session_start();
    //用于调取任务
    include 'sqlconnect.php';
    $pageSize = 10;   //每页显示的数量
    $rowCount = 0;   //要从数据库中获取
    $pageNow = 1;    //当前显示第几页
    $username=$_SESSION['username'];
    if(isset($_GET['id'])){
        $q=$_GET["id"];
    }
    else{
        @$q=l_q;
    }
    @$str=explode('_',$q,2);
    if(isset($str[0]) && $str[0]=='U'){
        $taskID=$str[1];
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                        <h4 class='modal-title' id='myModalLabel'>Upate Task Information</h4>
                    </div>
                    <div class='modal-body'>
                        <form class='form-horizontal' role='form'>
                            <div class='form-group'>
                            <label class='col-sm-3 home-label control-label'>Task Name</label>";
            $sql="SELECT *FROM task WHERE taskID=$taskID";
            $result=mysqli_query($conn,$sql);
            $w_hours=0;
            $unit_num=0;
            $notes=null;
            while($row=mysqli_fetch_array($result)){
                echo"<p class='col-sm-9'>".$row['taskName']."</p>
                    <input type='text' id='taskID' value='$taskID' style='display:none;'></input>
                    <input type='text' id='type' value='U' style='display:none;'></input>
                    </div>";
                if(!mysqli_fetch_array(mysqli_query($conn,"SELECT *FROM task_hierarchy WHERE taskID=$taskID"))){
                    $w_hours=$row['actualWLUs'];
                    echo"                                             
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Work Hours</label>
                                <div class='col-sm-9'>
                                    <input type='number' class='form-control' id='wlus' value='$w_hours'></input>
                                </div>
                            </div>";                   
                    $unit_num=$row['unitNumber'];
                    echo"<div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Unit Number</label>
                                <div class='col-sm-9'>
                                    <input type='number' class='form-control' id='unitnum' value='$unit_num'></input>
                                </div>
                            </div>";
                    $notes=$row['notes'];    
            
                                
                            echo"
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Notes</label>
                                <div class='col-sm-9'>
                                <textarea id='notes' class='form-control' rows='3'>$notes</textarea>
                                </div>
                            </div>
                        </form>
                    </div>           
                    <div class='modal-footer'>                   
                    <button id='update_btn' data-dismiss='modal' class='btn btn-primary' >Submit</button>
                    </div>    
                </div>
            </div>";
            }
        }
    }
    else if(isset($str[0]) && $str[0]=='F'){
        $taskID=$str[1];
        $taskName="";
        $startTime="";
        @$nowTime=date("Y-m-d H:i:s");
        $sql="SELECT taskName,startTime FROM task,allocated_task WHERE task.taskID=allocated_task.taskID and task.taskID=$taskID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskName=$row['taskName'];
            $startTime=$row['startTime'];
        }
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                        <h4 class='modal-title' id='myModalLabel'>Confirm completed</h4>
                    </div>
                    <div class='modal-body'>
                        <div style='display:none;' >
                            <input id='type' value='F'></input>
                            <input id='taskID' value='$taskID'></input>
                            <input id='current' value='$nowTime'></input>
                        </div>
                        <div class='table-responsive'>
                            <table class='table table-striped home-task-table'>
                               <thead>
                                    <tr>
                                        <th>Task Name</th>
                                        <th>Start time</th>
                                        <th>Current time</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        
                                        <td>$taskName</td>
                                        <td>$startTime</td>
                                        <td>$nowTime</td>
                                       
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class='modal-footer'>
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    <button type='submit' id='finish_btn' class='btn btn-primary' data-dismiss='modal'>Finish</button>
                    </div>    
                </div>
             </div>";
    }
    else if(isset($str[0]) && $str[0]=='V'){
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                        <h4 class='modal-title' id='myModalLabel'>Deteiled Information</h4>
                    </div>
                    <div class='modal-body'>";

        $taskid=$str[1];
        $sql="SELECT count(*) as num FROM task,allocated_task,workload.template where template.templateID=task.templateID and task.taskID=allocated_task.taskID and task.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
         while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        $sql="SELECT moduleCode,taskName,unitNumber,actualWLUs,username,startTime,endTime,isFinished,templateName,notes 
        FROM task,allocated_task,workload.template where template.templateID=task.templateID and task.taskID=allocated_task.taskID and task.taskID=$taskid";
        echo"<div class='table-responsive'>
                <table class='table table-striped home-task-table'>
                   
                    <tbody>";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){           
            if($num>0){
                echo"<tr>
                    <td class='td-label'>Task Name:</td>
                    <td>".$row['taskName']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Module Code:</td>
                    <td>".$row['moduleCode']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Unit Number:</td>
                    <td>".$row['unitNumber']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Work hours:</td>
                    <td>".$row['actualWLUs']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Username:</td>
                    <td>".$row['username']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Start time:</td>
                    <td>".$row['startTime']."</td>
                </tr>
                <tr>
                    <td class='td-label'>End time:</td>
                    <td>".$row['endTime']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Status:</td>";
                
                if($row['isFinished']==0){
                    echo"
                    <td>Unfinished</td>
                    </tr>";
                }else{
                    echo"<td>Finished</td></tr>";
                }
                echo"
                <tr>
                    <td class='td-label'>Template Name:</td>
                    <td>".$row['templateName']."</td>
                </tr>
                <tr>
                    <td class='td-label'>Notes:</td>
                    <td>".$row['notes']."</td>
                </tr>";
            }else{
                echo"<tr>
                    <td class='td-label'>Task Name:</td>
                    <td>".$row['taskName']."</td>
                </tr>
                <tr>
                    <td class='td-label'>ModuleCode:</td>
                    <td>".$row['moduleCode']."</td>
                </tr>
                <tr>
                    <td class='td-label'>templateName Name:</td>
                    <td>".$row['templateName']."</td>
                </tr>";
            }
            
            echo"</tbody>
        </table>
        </div>
        </div>
        <div class='modal-footer'>
                <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        </div>
        </div><!-- /.modal-content -->
        </div><!-- /.modal -->*/";
        }
    }
    else if(isset($str[0]) && $str[0]=='l' && $str[1]!='q'){
        $id=$str[1];
        $sql="SELECT task.taskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
        FROM task,allocated_task where task.taskID=allocated_task.taskID and task.taskID='$id'";
        $result=mysqli_query($conn,$sql);
       
        echo"<div class='table-responsive' >
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Total Work Hours</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
         while($row=mysqli_fetch_array($result)){
            $UID="U_".$row['taskID'];
            $FID="F_".$row['taskID'];
            $VID="V_".$row['taskID'];
            $taskID=$row['taskID'];
            $actWLUs=$row['actualWLUs'];
            $unitNum=$row['unitNumber'];
            $totalWorkHours=$actWLUs*$unitNum;
            echo"<tr id='$taskID'>
                <td>".$row['taskName']."</td>";
            if($row['actualWLUs']!=null){
                echo"<td>$totalWorkHours</td>";
            }else{
                echo"<td></td>";
            }
            echo"<td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
            if($row['actualWLUs']!=null){//为子任务情况
                if($row['isFinished']==0){//判断是否完成
                     echo"
                        <td><button  type='button'  class='jm-box' data-id='$FID'>Finish</button></td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
            }
            else{//为母任务情况下
                if($row['isFinished']==0){
                     echo"
                        <td>unFinish</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
            }
        }
        echo"</tbody>
        </table>
        </div>";
        $num="";
        $sql="SELECT count(1) AS num FROM task_hierarchy WHERE taskID='$id'";
        $t=mysqli_query($conn,$sql);
        while($row1=mysqli_fetch_array($t)){
            $num=$row1['num'];
        }
        if($num>0){
        echo"<div class='table-responsive'>
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Total Work Hours</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
            $sql="SELECT task_hierarchy.subtaskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
            FROM workload.task,workload.allocated_task,workload.task_hierarchy 
            where task.taskID=allocated_task.taskID and task.taskID=task_hierarchy.subtaskID and task_hierarchy.taskID='$id'";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                
                $UID="U_".$row['subtaskID'];
                $FID="F_".$row['subtaskID'];
                $VID="V_".$row['subtaskID'];
                $taskID=$row['subtaskID'];
                $actWLUs=$row['actualWLUs'];
                $unitNum=$row['unitNumber'];
                $totalWorkHours=$actWLUs*$unitNum;
                echo"<tr id='$taskID'>
                    <td>".$row['taskName']."</td>";
                if($row['actualWLUs']!=null){
                    echo"<td>$totalWorkHours</td>";
                }else{
                    echo"<td></td>";
                }
                    echo"<td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
                if($row['actualWLUs']!=null){//为子任务情况
                    if($row['isFinished']==0){//判断是否完成
                        echo"
                            <td><button  type='button'  class='jm-box' data-id='$FID'>Finish</button></td>
                            <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                        </tr>";
                    }
                    else{
                        echo"
                            <td>Finished</td>
                            <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                        </tr>";
                    }
                }
                else{//为母任务情况下
                    if($row['isFinished']==0){
                        echo"
                            <td>unFinish</td>
                            <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                        </tr>";
                    }
                    else{
                        echo"
                            <td>Finished</td>
                            <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                         </tr>";
                    }
                }
            }
        }
        echo"</tbody>
        </table>
        </div>";

    }
    else if(isset($str[0]) && $str[0]=='l' && $str[1]=='q'){
        if (!empty($_GET['pageNow'])){
         $pageNow = $_GET['pageNow'];
        }
        $pageCount = 0;  //表示共有多少页
        $sql1 = "SELECT count(1) from task,allocated_task WHERE task.taskID=allocated_task.taskID and username='$username' ";
        $res1 = mysqli_query($conn,$sql1);
        if($row1=mysqli_fetch_array($res1)){
            $rowCount = $row1[0];
        }
        //计算共有多少页，ceil取进1
        $pageCount = ceil(($rowCount/$pageSize));
        //使用sql语句时，注意有些变量应取出赋值。
         $pre = ($pageNow-1)*$pageSize;

        $result=mysqli_query($conn,"SELECT task.taskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
        FROM task,allocated_task where task.taskID=allocated_task.taskID and username='$username' limit $pre,$pageSize");
        echo"<div class='table-responsive' >
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Total Work Hours</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
        while($row=mysqli_fetch_array($result)){
            $UID="U_".$row['taskID'];
            $FID="F_".$row['taskID'];
            $VID="V_".$row['taskID'];
            $taskID=$row['taskID'];
            $actWLUs=$row['actualWLUs'];
            $unitNum=$row['unitNumber'];
            $totalWorkHours=$actWLUs*$unitNum;
            echo"<tr id='$taskID'>
                <td>".$row['taskName']."</td>";
            if($row['actualWLUs']!=null){
                echo"<td>$totalWorkHours</td>";
            }else{
                echo"<td></td>";
                }
                    echo"<td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
            if($row['actualWLUs']!=null){//为子任务情况
                if($row['isFinished']==0){//判断是否完成
                     echo"
                        <td><button  type='button'  class='jm-box' data-id='$FID'>Finish</button></td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
            }
            else{//为母任务情况下
                if($row['isFinished']==0){
                     echo"
                        <td>unFinish</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                     </tr>";
                }
            }
            
        }
        echo"</tbody>
        </table>";
        if($pageCount>1){
        echo"<div>
        <ul class='pager'>";       
        if($pageNow>1){
            if($pageNow==$pageCount){
                echo "<li class='next disabled'><a href='#'>Next</a></li>";
            }
        $prePage = $pageNow-1;
        echo "<li class='previous '><a class='pageContral' data-id='$prePage' >Previous</a></li>";
        
         }
         echo "{$pageNow}/{$pageCount}";
        if($pageNow<$pageCount){
            if($pageNow==1){
                echo "<li class='previous disabled'><a href='#'>Previous</a></li>";
            }
        $nextPage = $pageNow+1;
        echo "<li class='next '><a class='pageContral' data-id='$nextPage' >Next</a><li>";
        }
        echo "
        </ul>
        </div>
        <br/><br/>";
        }
    }
 ?>
